package 数据库.day2;

import 数据库.练习.DBUtil;

import java.sql.*;
// insert操作支持返回主键，写个例子。
public class ReturnTset {
    public static void main(String[] args) throws SQLException {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        con = DBUtil.getConnection();
        try {
            ps = con.prepareStatement("INSERT INTO `s_subject` VALUES (null , '专业猫狗1语', 42, 2)",PreparedStatement .RETURN_GENERATED_KEYS);
            ps.execute();
            rs = ps.getGeneratedKeys();
            while (rs.next()) {
                int ids= rs.getInt(1);
                System.out.println("主键值："+ids);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(rs,ps,con);
        }
    }
}
